package com.schoolCommunity.util;

import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class DeleteES {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    //传入memberId同步删除ES数据
    public boolean DeleteESMember(long memberId) throws IOException {

        DeleteRequest member = new DeleteRequest("member", Integer.toString((int) memberId));
        member.timeout("1s");

        DeleteResponse delete = client.delete(member, RequestOptions.DEFAULT);
//        System.out.println(delete.status());
        if (delete.status().toString().equals("OK")) {
            return true;
        } else {
            return false;
        }

    }

    //传入adminId同步删除ES数据
    public boolean DeleteESAdmin(long adminId) throws IOException {

        DeleteRequest admin = new DeleteRequest("admin", Integer.toString((int) adminId));
        admin.timeout("1s");

        DeleteResponse delete = client.delete(admin, RequestOptions.DEFAULT);
//        System.out.println(delete.status());
        if (delete.status().toString().equals("OK")) {
            return true;
        } else {
            return false;
        }

    }
}
